package com.java.rabbitmq.simple;

/**
 * @author Woo_home
 * @create 2020/5/26 12:36
 */

import com.java.rabbitmq.util.ConnectionUtils;
import com.rabbitmq.client.*;
import java.io.IOException;


/**
 * 消费者获取消息
 */
public class Receive {

    // 队列名称
    private static final String QUEUE_NAME = "test_simple_queue";

    public static void main(String[] args) throws Exception {

        // 获取连接
        Connection connection = ConnectionUtils.getConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 队列声明
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        DefaultConsumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String s = new String(body, "utf-8");
                System.out.println("receive : " + s);
            }
        };

        // 监听队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
